package com.apedad.quartz.config;

import com.apedad.quartz.job.SampleJob;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @author RocLiu [rocliu@tcl.com]
 * @version 1.0
 */
@Configuration
public class QuartzConfiguration {

    @Value("${quartz_cron_samplejob_expression}")
    public String quartz_cron_samplejob_expression;

    @Bean("sampleJob")
    public JobDetailFactoryBean sampleJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(SampleJob.class);
        jobDetailFactoryBean.setGroup("job");
        jobDetailFactoryBean.setName("sampleJob");
        return jobDetailFactoryBean;
    }

    @Bean("sampleTrigger")
    public CronTriggerFactoryBean sampleTrigger(@Qualifier("sampleJob") JobDetail jobDetail) {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(jobDetail);
        cronTriggerFactoryBean.setCronExpression(quartz_cron_samplejob_expression);
        return cronTriggerFactoryBean;
    }

    @Bean("sampleScheduler")
    public SchedulerFactoryBean sampleScheduler(@Qualifier("sampleTrigger") Trigger trigger){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setAutoStartup(false);
        schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
        schedulerFactoryBean.setTriggers(trigger);
        return schedulerFactoryBean;
    }
}
